home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.02 Feb 97 / Getting Started, ShapeWorld / ShapeWorld.java < prev    next >
Encoding:
Java Source  |  1996-11-07  |  2.8 KB  |  156 lines  |  [TEXT/CWIE]

  1. import java.awt.*;
  2. import java.util.*;
  3.  
  4. abstract class Shape
  5. {
  6.     boolean        highlighted;
  7.     ShapeCanvas    shapeCanvas;
  8.     int            shapeX, shapeY, shapeWidth, shapeHeight;
  9.     Rectangle    boundsRect;
  10.     
  11.     Shape( ShapeCanvas canv, int x, int y, int width, int height )
  12.     {
  13.         shapeCanvas = canv;
  14.         highlighted = false;
  15.         
  16.         shapeX = x;
  17.         shapeY = y;
  18.         shapeWidth = width;
  19.         shapeHeight = height;
  20.         
  21.         boundsRect = new Rectangle( x, y, width, height );
  22.     }
  23.     
  24.     abstract public void draw( Graphics g );
  25.     
  26.     public void setHighlight( boolean newHighlight )
  27.     {
  28.         highlighted = newHighlight;
  29.     }
  30.     
  31.     public boolean isHighlighted()
  32.     {
  33.         return highlighted;
  34.     }
  35.     
  36.     public boolean isPointInShape( int x, int y )
  37.     {
  38.         return boundsRect.inside( x, y );
  39.     }
  40. }
  41.  
  42. class RectShape extends Shape
  43. {
  44.     RectShape( ShapeCanvas canv, int x, int y, int width, int height )
  45.     {
  46.         super( canv, x, y, width, height );
  47.     }
  48.     
  49.     public void draw( Graphics g )
  50.     {
  51.         if ( isHighlighted() )
  52.         {
  53.             g.setColor( Color.black );
  54.             g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
  55.             g.setColor( Color.red );
  56.             g.fillRect( shapeX+2, shapeY+2, shapeWidth-4, shapeHeight-4 );
  57.         }
  58.         else
  59.         {
  60.             g.setColor( Color.red );
  61.             g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
  62.         }
  63.     }
  64. }
  65.  
  66. class ShapeCanvas extends Canvas
  67. {
  68.     Vector        shapes;
  69.     Shape        curShape;
  70.     
  71.     ShapeCanvas( int width, int height )
  72.     {
  73.         shapes = new Vector();
  74.         curShape = null;
  75.         
  76.         setBackground( Color.yellow );
  77.         
  78.         resize( width, height );
  79.     }
  80.     
  81.     public void addShape( Shape newShape )
  82.     {
  83.         shapes.addElement( newShape );
  84.     }
  85.     
  86.     public void paint( Graphics g )
  87.     {
  88.         for ( Enumeration e = shapes.elements(); e.hasMoreElements(); )
  89.         {
  90.             Shape s = (Shape)e.nextElement();
  91.             s.draw( g );
  92.         }
  93.     }
  94.     
  95.     public Shape findInShapeList( int x, int y )
  96.     {
  97.         for ( Enumeration e = shapes.elements(); e.hasMoreElements(); )
  98.         {
  99.             Shape s = (Shape)e.nextElement();
  100.             
  101.             if ( s.isPointInShape( x, y ) )
  102.             {
  103.                 s.setHighlight( ! s.isHighlighted() );
  104. s.draw( getGraphics() );
  105. //                repaint();
  106.                 return s;
  107.             }
  108.         }
  109.         
  110.         return null;
  111.     }
  112.     
  113.     public void update (Graphics g)
  114.     {
  115.             paint(g);
  116.     }
  117.     
  118.     public boolean mouseDown( Event e, int x, int y )
  119.     {
  120.         curShape = findInShapeList( x, y );
  121.         
  122.         return true;
  123.     }
  124. }
  125.  
  126. public class ShapeWorld extends java.applet.Applet
  127. {
  128.     ShapeCanvas    sCanvas;
  129.     final int    shapeWidth = 20;
  130.     final int    shapeHeight = 20;
  131.     
  132.     public void init()
  133.     {
  134.         int            x, y;
  135.         
  136.         sCanvas = new ShapeCanvas( 440, 290 );
  137.         add( sCanvas );
  138.         
  139.         Random ran = new Random();
  140.         Rectangle b = sCanvas.bounds();
  141.         
  142.         for ( int i=1; i<=10; i++ )
  143.         {
  144.             x = b.x + (int)((float)(b.width) * ran.nextFloat() );
  145.             if ( x > b.x + b.width - shapeWidth )
  146.                 x -= shapeWidth;
  147.                 
  148.             y = b.y + (int)((float)(b.height) * ran.nextFloat() );
  149.             if ( y > b.y + b.height - shapeHeight )
  150.                 y -= shapeHeight;
  151.             
  152.             RectShape r = new RectShape( sCanvas, x, y, shapeWidth, shapeHeight );
  153.             sCanvas.addShape( r );
  154.         }
  155.     }
  156. }